/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator;

import net.db.generator.JavaBaseClassWriter;

/**
 *
 * @author abosancic
 */
public class JSFUtilClassWriter extends JavaBaseClassWriter {

    public JSFUtilClassWriter() {
        super();
        importPackages = new String[]{
            "java.util.List",
            "javax.faces.application.FacesMessage",
            "javax.faces.component.UIComponent",
            "javax.faces.context.FacesContext",
            "javax.faces.convert.Converter",
            "javax.faces.model.SelectItem"};
    }

    @Override
    protected void generateClassName() {
        stringClass.append("public class " + getClassName() + " {\n\n");
        generateDAOClass();
    }

    //==========================================================================
    // setters and getters
    //==========================================================================
    @Override
    public String getClassName() {
        return "JsfUtil";
    }

    protected void generateDAOClass() {

        String daoClass = spaces + "public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {\n"
                + "        int size = selectOne ? entities.size() + 1 : entities.size();\n"
                + "        SelectItem[] items = new SelectItem[size];\n"
                + "        int i = 0;\n"
                + "        if (selectOne) {\n"
                + "            items[0] = new SelectItem(\"\", \"---\");\n"
                + "            i++;\n"
                + "        }\n"
                + "        for (Object x : entities) {\n"
                + "            items[i++] = new SelectItem(x, x.toString());\n"
                + "        }\n"
                + "        return items;\n"
                + "    }\n"
                + "\n"
                + "    public static void addErrorMessage(Exception ex, String defaultMsg) {\n"
                + "        String msg = ex.getLocalizedMessage();\n"
                + "        if (msg != null && msg.length() > 0) {\n"
                + "            addErrorMessage(msg);\n"
                + "        } else {\n"
                + "            addErrorMessage(defaultMsg);\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public static void addErrorMessages(List<String> messages) {\n"
                + "        for (String message : messages) {\n"
                + "            addErrorMessage(message);\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public static void addErrorMessage(String msg) {\n"
                + "        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);\n"
                + "        FacesContext.getCurrentInstance().addMessage(null, facesMsg);\n"
                + "    }\n"
                + "\n"
                + "    public static void addSuccessMessage(String msg) {\n"
                + "        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);\n"
                + "        FacesContext.getCurrentInstance().addMessage(\"successInfo\", facesMsg);\n"
                + "    }\n"
                + "\n"
                + "    public static String getRequestParameter(String key) {\n"
                + "        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);\n"
                + "    }\n"
                + "\n"
                + "    public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) {\n"
                + "        String theId = JsfUtil.getRequestParameter(requestParameterName);\n"
                + "        return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);\n"
                + "    }\n";
        stringClass.append(daoClass);
    }

    @Override
    protected void generateFields() {
    }

    @Override
    protected void generateToString() {
    }

    @Override
    protected void generateHashCode() {
    }

    @Override
    protected void generateEquals() {
    }

    @Override
    protected void generateConstructor() {
    }

    @Override
    protected void generateGettersAndSetters() {
    }
}
